import java.sql.*;
import java.sql.Connection;
public class DatabaseHelper {

    // MySQL 数据库连接信息
    private static final String DB_URL = "jdbc:mysql://localhost:3306/restaurant";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "020613";

    // 获取数据库连接
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
    }

    // 根据用户名查询用户信息（包括密码和角色）
    public static String[] getUserByUsername(String username) {
        String query = "SELECT password, role FROM users WHERE username = ?";

        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement(query)) {

            ps.setString(1, username);
            ResultSet rs = ps.executeQuery();

            if (rs.next()) {
                String password = rs.getString("password");
                String role = rs.getString("role");
                return new String[]{password, role};  // 返回密码和角色
            } else {
                System.out.println("User not found: " + username);  // 调试信息
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;  // 用户不存在
    }

}